package com.hzjh.h5mall.ui.preview

import android.content.pm.ActivityInfo
import android.content.res.TypedArray
import android.os.Build
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.frame.JHBaseVM
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.h5mall.databinding.ActivityPreviewVideoBinding
import com.hzjh.service.videolib.BaseVideoActivity
import com.hzjh.service.videolib.widget.SampleCoreVideo
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder
import com.shuyu.gsyvideoplayer.utils.GSYVideoType

@Route(path = RouteUrl.PREVIEW_ACTIVITY_VIDEO)
class PreviewVideoActivity : BaseVideoActivity<ActivityPreviewVideoBinding,JHBaseVM>() {
    @JvmField
    @Autowired(name = RouteUrl.PARAMS_VIDEO_URL)
    var mVideoUrl: String = "" //视频url

    override fun initData() {

    }

    override fun initView() {
        viewBinding.ivClose.setOnClickListener {
            finish()
        }
        initVideoBuilderMode()
        //设置循环播放
        viewBinding.videoPlayer.isLooping = true
        playVideo()
    }

    override fun initViewBinding(): ActivityPreviewVideoBinding {
        return ActivityPreviewVideoBinding.inflate(layoutInflater)
    }

    override fun getGSYVideoPlayer(): SampleCoreVideo {
        return viewBinding.videoPlayer
    }

    override fun getGSYVideoOptionBuilder(): GSYVideoOptionBuilder {
        GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_FULL) //全屏
//        GSYVideoType.setShowType(GSYVideoType.SCREEN_TYPE_DEFAULT)
        return GSYVideoOptionBuilder()
//            .setThumbImageView(imageView)
            .setUrl(mVideoUrl)
            .setCacheWithPlay(true)
            .setVideoTitle(" ")
            .setIsTouchWiget(true)
            .setRotateViewAuto(false)
            .setLockLand(false)
            .setShowFullAnimation(false)
            .setNeedLockFull(true)
            .setSeekRatio(1f)
    }

    override fun clickForFullScreen() {

    }

    override fun getDetailOrientationRotateAuto(): Boolean {
        return false
    }

    override fun setRequestedOrientation(requestedOrientation: Int) {
        //彻底解决Android8.0 Only fullscreen opaque activities can request orientation
        if (Build.VERSION.SDK_INT == Build.VERSION_CODES.O && isTranslucentOrFloating()) {
            return
        }
        super.setRequestedOrientation(requestedOrientation)
    }

    private fun isTranslucentOrFloating(): Boolean {
        var isTranslucentOrFloating = false
        try {
            val styleableRes = Class.forName("com.android.internal.R\$styleable")
                .getField("Window")[null] as IntArray
            val ta = obtainStyledAttributes(styleableRes)
            val m =
                ActivityInfo::class.java.getMethod(
                    "isTranslucentOrFloating",
                    TypedArray::class.java
                )
            m.isAccessible = true
            isTranslucentOrFloating = m.invoke(null, ta) as Boolean
            m.isAccessible = false
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return isTranslucentOrFloating
    }
}